package cn.javacv.gulimall.product.service.impl;

import cn.javacv.gulimall.product.dao.BrandDao;
import cn.javacv.gulimall.product.dao.CategoryBrandRelationDao;
import cn.javacv.gulimall.product.dao.CategoryDao;
import cn.javacv.gulimall.product.entity.BrandEntity;
import cn.javacv.gulimall.product.entity.CategoryEntity;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.javacv.gulimall.common.utils.PageUtils;
import cn.javacv.gulimall.common.utils.Query;

import cn.javacv.gulimall.product.entity.CategoryBrandRelationEntity;
import cn.javacv.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public CategoryBrandRelationEntity saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
        return categoryBrandRelation;
    }

    @Override
    public boolean updateBrandRelation(long brandId, String brandName) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(brandName);
        return this.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public boolean updateCategoryRelation(long categoryId, String categoryName) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(categoryId);
        categoryBrandRelationEntity.setBrandName(categoryName);
        return this.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",categoryId));
    }

    @Override
    public List<BrandEntity> listBrandByCategoryId(Long catId) {
        List<CategoryBrandRelationEntity> list = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
        List<Long> brandIdList = list.stream().map(
                (categoryBrandRelationEntity) -> {return categoryBrandRelationEntity.getBrandId();}
        ).collect(Collectors.toList());
        return brandDao.selectBatchIds(brandIdList);
    }

}
